<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>编辑物品出库</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">


        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>物品出库单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>出库日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="dt" id="dt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>领取人</label>
                        <div class="layui-input-block">
                            <input type="text" id="baner" name="baner" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </div>


        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>出库明细</span></div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>名称及规格</th>
                            <th>单位</th>
                            <th>入库数量</th>
                            <th>累计出库数量</th>
                            <th>库存数量</th>
                            <th><text>*</text>本次出库数量</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>

    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;

    $("#commentForm").validate({
        rules: {
            dt: {required: true},
            baner: {required: true},
        },
        messages:{
            dt: {required: "请选择"},
            baner: {required: "请输入"},
        }
    });


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_wu_guan_chu_edit, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;

                $("#number").val(data.number);
                $("#dt").val(data.dt);
                $("#baner").val(data.baner);


                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" readonly class="layui-input name disabled"></td>';
                listAddStr += '<td><input type="text" readonly class="layui-input unit disabled"></td>';
                listAddStr += '<td><input type="number" readonly class="layui-input runum disabled"></td>';
                listAddStr += '<td><input type="number" readonly class="layui-input chunum disabled"></td>';
                listAddStr += '<td><input type="number" readonly class="layui-input kucun disabled"></td>';
                listAddStr += '<td><input type="number" class="layui-input num"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);


                var list = [res.data];
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" id="'+list[key].id+'" readonly class="layui-input name disabled" value="'+list[key].name+'"></td>';
                        str += '<td><input type="text" readonly class="layui-input unit disabled" value="'+list[key].unit+'"></td>';
                        str += '<td><input type="number" readonly class="layui-input runum disabled" value="'+list[key].runum+'"></td>';
                        str += '<td><input type="number" readonly class="layui-input chunum disabled" value="'+list[key].chunum+'"></td>';
                        str += '<td><input type="number" readonly class="layui-input kucun disabled" value="'+list[key].kucun+'"></td>';
                        str += '<td><input type="number" class="layui-input num" value="'+list[key].num+'"></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }
                update_refresh_row_func();

                render_from();
                initParseMoney();
            }
        });


        form.on('submit(add)', function(data){
            parseMoney(data);
            var validRes = $("#commentForm").valid();
            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var id = $(this).find('.name').attr('id');
                    var name = $(this).find('.name').val();
                    var unit = $(this).find('.unit').val();
                    var kucun = $(this).find('.kucun').val();
                    var num = $(this).find('.num').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!num){
                        throw new Error('第'+index+'行，请输入本次出库数量');
                    }
                    if(num > kucun){
                        throw new Error('第'+index+'行，本次出库数量不能大于库存数量');
                    }

                    list.push({
                        id:id,
                        name:name,
                        unit:unit,
                        num:num,
                        explain:explain,
                        dt:data.field.dt,
                        baner:data.field.baner,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data = parseMoney(data);
            facade.ajax({url: ask_wu_guan_chu_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });



    function update_refresh_row_func() {
        render_date();
        render_from();

        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }

</script>
</body>
</html>
